package com.macro.mall.service.impl;

import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsMuseumDetail;
import com.macro.mall.dto.BmsMuseumParam;
import com.macro.mall.mapper.BmsMuseumMapper;
import com.macro.mall.model.BmsMuseum;
import com.macro.mall.model.BmsMuseumExample;
import com.macro.mall.query.BmsMuseumQuery;
import com.macro.mall.service.BmsMuseumPicService;
import com.macro.mall.service.BmsMuseumService;
import com.macro.mall.utils.AdminUtils;

@Service
public class BmsMuseumServiceImpl implements BmsMuseumService {

    @Autowired
    private BmsMuseumMapper bmsMuseumMapper;

    @Autowired
    private BmsMuseumPicService bmsMuseumPicService;

    @Override
    public List<BmsMuseum> getList(BmsMuseumQuery query) {
        Integer pageNum = query.getPageNum();
        Integer pageSize = query.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        BmsMuseumExample example = new BmsMuseumExample();
        BmsMuseumExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo((byte) 0);
        if (StringUtils.isNotBlank(query.getName())) {
            criteria.andNameLike("%" + query.getName() + "%");
        }
        if (Objects.nonNull(query.getOpeningTime())) {
            criteria.andOpeningTimeLessThanOrEqualTo(query.getOpeningTime());
        }
        if (Objects.nonNull(query.getClosingTime())) {
            criteria.andClosingTimeGreaterThanOrEqualTo(query.getClosingTime());
        }


        example.setOrderByClause("create_time desc");
        List<BmsMuseum> list = bmsMuseumMapper.selectByExample(example);
        return list;
    }

    @Override
    public int create(BmsMuseumParam bmsMuseumParam) {
        BmsMuseum bmsMuseum = new BmsMuseum();
        BeanUtils.copyProperties(bmsMuseumParam, bmsMuseum);
        AdminUtils.setCreation(bmsMuseum);
        List<String> selecteDays = bmsMuseumParam.getSelectedDays();
        if (selecteDays != null && !selecteDays.isEmpty()) {
        	String daysStr = String.join(",", selecteDays);
        	bmsMuseum.setSelectedDays(daysStr);
        }
        int count = bmsMuseumMapper.insertSelective(bmsMuseum);

        String coverPic = bmsMuseumParam.getCoverPic();
        List<String> picList = bmsMuseumParam.getPicList();
        bmsMuseumPicService.create(bmsMuseum.getId(), coverPic, picList);
        return count;
    }

    @Override
    public int update(BmsMuseumParam bmsMuseumParam) {
        BmsMuseum bmsMuseum = new BmsMuseum();
        bmsMuseum.setId(bmsMuseumParam.getId());
        BeanUtils.copyProperties(bmsMuseumParam, bmsMuseum);
        AdminUtils.setEdition(bmsMuseum);
        
        List<String> selecteDays = bmsMuseumParam.getSelectedDays();
        if (selecteDays != null && !selecteDays.isEmpty()) {
        	String daysStr = String.join(",", selecteDays);
        	bmsMuseum.setSelectedDays(daysStr);
        }
        int conut = bmsMuseumMapper.updateByPrimaryKeySelective(bmsMuseum);

        bmsMuseumPicService.deleteByMuseumId(bmsMuseumParam.getId());

        String coverPic = bmsMuseumParam.getCoverPic();
        List<String> picList = bmsMuseumParam.getPicList();
        bmsMuseumPicService.create(bmsMuseumParam.getId(), coverPic, picList);

        return conut;
    }

    @Override
    public BmsMuseumDetail getItem(Long id) {
        BmsMuseum bmsMuseum = bmsMuseumMapper.selectByPrimaryKey(id);
        BmsMuseumDetail detail = new BmsMuseumDetail();
        BeanUtils.copyProperties(bmsMuseum, detail);
        // 获取封面图
        String coverPic = bmsMuseumPicService.findCoverPicByMuseumId(bmsMuseum.getId());
        // 获取活动图片
        List<String> picList = bmsMuseumPicService.findPicListByMuseumId(bmsMuseum.getId());

        detail.setCoverPic(coverPic);
        detail.setPicList(picList);

        return detail;
    }

    @Override
    public int delete(List<Long> ids) {
        BmsMuseumExample example = new BmsMuseumExample();
        example.createCriteria().andIdIn(ids);
        BmsMuseum bmsMuseum = new BmsMuseum();
        AdminUtils.setDelete(bmsMuseum);
        return bmsMuseumMapper.updateByExampleSelective(bmsMuseum, example);
    }

    @Override
    public List<BmsMuseum> findAllList() {
        return bmsMuseumMapper.findAllList();
    }

}
